package com.groot.medical.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.groot.common.controller.BaseServletController;
import com.groot.medical.entity.Menu;
import com.groot.medical.entity.PageBean;
import com.groot.medical.entity.PaySetting;
import com.groot.medical.service.PaySettingService;
import com.groot.medical.service.PaySettingServiceImpl;
@WebServlet("/sys/paysetting.do")
public class PaySettingController extends BaseServletController{
	private static final long serialVersionUID = 1L;
	private PaySettingService paySettingService = new PaySettingServiceImpl();
	private static Logger LOGGER = Logger.getLogger(PaySettingController.class);
	
	
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("this is @WebServlet(\"/index.do\").execute()");
		return null;
	}
	/**
	 * 分页查询参合登记设置信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findPaySettingListByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		List<Menu> menus=(List<Menu>) request.getSession().getAttribute("menuList");
		List<Menu> result = new ArrayList<Menu>();
		/*根据页面传过来的id与该用户所有权限的上一级id比较
		 * 若该权限的上一级传过来的id相同则存入新集合
		 */
		for(Menu menu: menus){
			if(menu.getMenuPid().equals(tgId)){
				result.add(menu);
			}
		}
		
		Gson gson = new Gson();
		request.setAttribute("resultMenus", gson.toJson(result));
		request.setAttribute("tgId", tgId);
		
		int pageSize = 2;
		PageBean areaPage = paySettingService.findPaySettingListByPage(pageNum, pageSize);
		request.setAttribute("areaPage", areaPage);
		return "/WEB-INF/page/paysetting/paysetting_list.jsp";
	}
	/**
	 * 打开参合缴费设置添加页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String toAddPaySettingPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setAttribute("tgId",request.getParameter("tgId"));
		
		return "/WEB-INF/page/paysetting/paysetting_add.jsp";
	}
	
	/**
	 * 添加参合缴费设置信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addPaySettingInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String money = request.getParameter("money");
		
		//生成时间
		Date now = new Date();
		SimpleDateFormat year = new SimpleDateFormat("yyyy");//设置日期格式
		String nowTime = year.format(now);
		//处理时间格式
		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		Date startdate = null;
		try {
			startdate = time.parse(startTime);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Date enddate = null;
		try {
			enddate = time.parse(endTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(startTime);
		//封装
		PaySetting paysetting = new PaySetting();
		paysetting.setCureentYear(nowTime);
		paysetting.setStartTime(startdate);
		paysetting.setEndTime(enddate);
		paysetting.setPayMonry(money);
		//执行添加
		paySettingService.addPaySettingInfo(paysetting);
		request.setAttribute("tgId",request.getParameter("tgId"));
		request.setAttribute("pageNum",request.getParameter("pageNum"));
		return findPaySettingListByPage(request,response);
	}	
	/**
	 * 打开参合缴费设置信息修改页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
		public String toEditPaySettingPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String year = request.getParameter("id");
			request.setAttribute("tgId",request.getParameter("tgId"));
			//查询要修改的信息
			PaySetting paySetting= paySettingService.findPaySettingByYear(year);
			request.setAttribute("paySetting",paySetting);
			return "/WEB-INF/page/paysetting/paysetting_edit.jsp";
	}
		/**
		 * 添加参合缴费设置信息
		 * @param request
		 * @param response
		 * @return
		 * @throws ServletException
		 * @throws IOException
		 */
		public String editPaySettingInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String money = request.getParameter("money");
			String currentYear = request.getParameter("cureentYear");
			
			
			
			//处理时间格式
			SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
			Date startdate = null;
			try {
				startdate = time.parse(startTime);
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Date enddate = null;
			try {
				enddate = time.parse(endTime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(startTime);
			//封装
			PaySetting paysetting = new PaySetting();
			paysetting.setCureentYear(currentYear);
			paysetting.setStartTime(startdate);
			paysetting.setEndTime(enddate);
			paysetting.setPayMonry(money);
			//执行添加
			paySettingService.editPaySettingInfo(paysetting);
			request.setAttribute("tgId",request.getParameter("tgId"));
			request.setAttribute("pageNum",request.getParameter("pageNum"));
			return findPaySettingListByPage(request,response);
		}	
		
}
